<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\shiyao;

use app\store\controller\Controller;
use app\store\model\shiyao\ProjectTestee as ProjectTesteeModel;
use think\facade\Db;

/**
 * 项目控制器
 * Class StoreUser
 * @package app\store\controller
 */
class Testee extends Controller {

	public function listAll() {
		$model = new ProjectTesteeModel;
		$list = $model->getListAll($this->request->param());
		return $this->renderSuccess(compact('list'));
	}
	public function detail(int $testeeId) {

		$detail = ProjectTesteeModel::detail($testeeId);
		return $this->renderSuccess(compact('detail'));
	}

	/**
	 * 列表
	 * @return array
	 * @throws \think\db\exception\DbException
	 */
	public function list() {
		$model = new ProjectTesteeModel;
		$list = $model->getList($this->request->param());
		return $this->renderSuccess(compact('list'));
	}

	/**
	 *
	 * @return array
	 */
	public function add() {
		// 新增记录
		$model = new ProjectTesteeModel;
		if ($model->add($this->postForm())) {
			return $this->renderSuccess('添加成功');
		}
		return $this->renderError($model->getError() ?: '添加失败');
	}

	/**
	 *
	 * @param int $projectId
	 * @return array
	 */
	public function edit(int $testeeId) {
		// 管理员详情
		$model = ProjectTesteeModel::detail($testeeId);

		//查询补助申请
		$log = Db::name('shiyao_project_pay_log')->where('testee_id', $testeeId)->find();
		if ($log) {
			return $this->renderError('有补助记录，无法修改');
		}

		// 更新记录
		if ($model->edit($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError() ?: '更新失败');
	}

	public function audit(int $projectId) {
		// 管理员详情
		$model = ProjectTesteeModel::detail($projectId);
		// 更新记录
		if ($model->audit($this->postForm())) {
			return $this->renderSuccess('审核成功');
		}
		return $this->renderError($model->getError() ?: '审核失败');
	}

	public function delete(array $logIds) {
		$model = new ProjectTesteeModel;
		if (!$model->setDelete($logIds)) {
			return $this->renderError($model->getError() ?: '删除失败');
		}
		return $this->renderSuccess('删除成功');
	}

}
